/**
 * 
 */
package com.shine.icis.blo.imp;

import com.shine.cotrun.sql.QueryParameters;
import com.shine.cotrun.web.comm.ListPage;
import com.shine.icis.blo.IDeptInfoService;
import com.shine.icis.dao.IDeptInfoDAO;
import com.shine.icis.dto.DeptInfo;
import com.shine.icis.dto.DeptInfo.DeptInfoId;
import com.shine.icis.exception.ICISException;

/**
 * @author zsy
 *
 */
public class DeptInfoService implements IDeptInfoService {
	
	private IDeptInfoDAO deptInfoDAO;

	
	public void setDeptInfoDAO(IDeptInfoDAO deptInfoDAO) {
		this.deptInfoDAO = deptInfoDAO;
	}

	@Override
	public void addDeptInfo(String deptCode, String deptName,
			String intDeptCode, String occurDate) throws ICISException {
		if (deptCode == null || intDeptCode == null) {
			throw new IllegalArgumentException();
		}
		DeptInfo info = new DeptInfo();
		DeptInfoId id = info.new DeptInfoId();
//		id.setCompanyCode(companyCode);
		id.setDeptCode(deptCode);
		id.setIntDeptCode(intDeptCode);
		if (!deptInfoDAO.checkId(id)) {
			throw new ICISException("证券公司代码、营业部代码、营业部内部代码不能重复");
		}
		info.setId(id);
		info.setDeptName(deptName);
		info.setOccurDate(occurDate);
		info.setRecordNo(deptInfoDAO.findNextRecordNo());
		deptInfoDAO.save(info);
		
	}
	
	@Override
	public void modifyDeptInfo(String oldDeptCode, String oldIntDeptCode, 
			String deptCode, String deptName,
			String intDeptCode, String occurDate) throws ICISException {
		if (oldDeptCode == null || deptCode == null
				|| oldIntDeptCode == null || intDeptCode == null) {
			throw new IllegalArgumentException();
		}
		DeptInfo info = new DeptInfo();
		DeptInfoId id = info.new DeptInfoId();
//		id.setCompanyCode(companyCode);
		id.setDeptCode(deptCode);
		id.setIntDeptCode(intDeptCode);
		if (!(oldDeptCode.equals(deptCode) 
				&& oldIntDeptCode.equals(intDeptCode)) 
				&& !deptInfoDAO.checkId(id)) {
			throw new ICISException("证券公司代码、营业部代码、营业部内部代码不能重复");
		}
		info.setId(id);
		info.setDeptName(deptName);
		info.setOccurDate(occurDate);
		
		DeptInfoId oldId = info.new DeptInfoId();
//		oldId.setCompanyCode(oldCompanyCode);
		oldId.setDeptCode(oldDeptCode);
		oldId.setIntDeptCode(oldIntDeptCode);
		deptInfoDAO.update(oldId, info);
		
	}

	@Override
	public void deleteDeptInfo(String deptCode, String intDeptCode) {
		if (deptCode == null || intDeptCode == null) {
			return;
		}
		DeptInfoId id = new DeptInfo().new DeptInfoId();
//		id.setCompanyCode(companyCode);
		id.setDeptCode(deptCode);
		id.setIntDeptCode(intDeptCode);
		deptInfoDAO.delete(id);
		
	}

	@Override
	public ListPage<DeptInfo> getDeptInfoPage(QueryParameters qp) {
		if (qp == null) {
			throw new IllegalArgumentException();
		}
		return deptInfoDAO.findListPage(qp);
	}

}
